package com.example.dangerouscargodriver.ui.fragment.message;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.dangerouscargodriver.ui.fragment.tab.MessageTabFragment;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.ui.fragment.tab.MessageTabOrderInfoFragmentWrapper;
import com.example.dangerouscargodriver.ui.fragment.tab.MessageTabSystemFragmentWrapper;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MessageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MessageFragment extends Fragment {

    private static final String TAG = "MessageFragment";
    private String[] tabs = {"订单消息", "系统通知"};
    private List<MessageTabFragment> tabFragmentList = new ArrayList<>();
    private View mRoot = null;

    public MessageFragment() {
        Log.d(TAG, "MessageFragment ++++++");
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment MessageFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MessageFragment newInstance() {
        MessageFragment fragment = new MessageFragment();
        Bundle args = new Bundle();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mRoot = inflater.inflate(R.layout.fragment_message, container, false);
        TabLayout tabLayout = mRoot.findViewById(R.id.tab_layout);
        ViewPager viewPager = mRoot.findViewById(R.id.view_pager);

        //添加tab
        tabLayout.addTab(tabLayout.newTab().setText(tabs[0]));
        tabFragmentList.add( new MessageTabOrderInfoFragmentWrapper());
        tabLayout.addTab(tabLayout.newTab().setText(tabs[1]));
        tabFragmentList.add( new MessageTabSystemFragmentWrapper());

        viewPager.setAdapter(new FragmentStatePagerAdapter(getActivity().getSupportFragmentManager(),
                FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {

                return tabFragmentList.get(position);
            }

            @Override
            public int getCount() {

                return tabFragmentList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {

                return tabs[position];
            }
        });

        //设置TabLayout和ViewPager联动
        tabLayout.setupWithViewPager(viewPager,true);
        return mRoot;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}